package com.si.controller.web;

import com.si.entity.FileDownload;
import com.si.entity.InfoEnclosure;
import com.si.service.InfoEnclosureService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.si.entity.Info;
import com.si.entity.InfoTypeClassA;
import com.si.service.InfoService;
import com.si.service.InfoTypeClassAService;
import com.si.util.HTMLTag;
import com.si.util.Paging;
import com.si.util.QueryWhere;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 信息
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/info")
public class InfoController {

	@Autowired
	private InfoService infoService;
	@Autowired
	private InfoTypeClassAService infoTypeClassAService;
	@Autowired
	private InfoEnclosureService infoEnclosureService;
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value="list/{id}")
	public String list(ModelMap modelMap,
			@PathVariable(value="id") Integer infoTypeId,
			@RequestParam(value = "curPage", defaultValue = "0", required = false) Integer curPage,
			@RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
		QueryWhere.push(Restrictions.eq("infoTypeClassA.id",infoTypeId));
		QueryWhere.setOrderList(Order.desc("createDate"));
		Paging<Info> paging = infoService.createCriteriaPaging(curPage, pageSize);
		for (Info info:paging.getData()) {
			info.setCnContent(HTMLTag.delHTMLTag(info.getCnContent()));
		}
		modelMap.put("paging", paging);

		/*根据infoTypeId到对应infoType表中获取标题*/
		InfoTypeClassA infoA= (InfoTypeClassA)infoTypeClassAService.getObjectById(infoTypeId);
		modelMap.put("infoA", infoA);

		return "webPage/info/list";
	}
	
	/**
	 * 搜索
	 * @return
	 */
	@RequestMapping(value="search")
	public String search(ModelMap modelMap,
			@RequestParam(value = "curPage", defaultValue = "0", required = false) Integer curPage,
			@RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
			@RequestParam(value = "keywords", required = false) String keywords) {
		if(StringUtils.isNoneBlank(keywords)) {
			QueryWhere.push(Restrictions.like("cnContent", "%" + keywords + "%", MatchMode.ANYWHERE));
		}
		QueryWhere.setOrderList(Order.desc("createDate"));
		Paging<Info> paging = infoService.createCriteriaPaging(curPage, pageSize);
		for (Info info:paging.getData()) {
			info.setCnContent(HTMLTag.delHTMLTag(info.getCnContent()));
		}
		modelMap.put("paging", paging);
		modelMap.put("keywords", keywords);
		
		/*根据infoTypeId到对应infoType表中获取标题*/
		InfoTypeClassA infoA= new InfoTypeClassA();
		infoA.setName("搜索");
		modelMap.put("infoA", infoA);

		return "webPage/info/search";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value="view/{id}")
	public String view(ModelMap modelMap, @PathVariable(value="id") Integer id) {
		Info info=(Info)infoService.getObjectById(id);
		info.setVolume(info.getVolume()+1);
		infoService.update(info);
		modelMap.put("info", info);
		/*根据infoTypeId到对应infoType表中获取标题*/
		InfoTypeClassA infoA= (InfoTypeClassA)infoTypeClassAService.getObjectById(info.getInfoTypeClassA().getId());
		HashMap<String, Object> map = new HashMap<>();
		map.put("infoid",info.getId());
		List<InfoEnclosure> all = infoEnclosureService.getAll(map,null);
		modelMap.put("infofile",all);
		modelMap.put("infoA", infoA);
		return "webPage/info/view";
	}


	@RequestMapping(value = "/download/{id}", method = RequestMethod.GET) //匹配的是href中的download请求
	public ResponseEntity<byte[]> download(HttpServletRequest request, @PathVariable(value = "id") Integer id,
																				 Model model, HttpSession session) throws IOException {

		//String downloadFilePath = "D:\\userUploadFile\\Files";//从我们的上传文件夹中去取
		InfoEnclosure fileDownload = (InfoEnclosure) infoEnclosureService.getObjectById(id);
		String downloadFilePath = fileDownload.getUrl();
		//获取文件路径
		String path = session.getServletContext().getRealPath("/upload/info_enclosuer/");
		System.out.println(path);
		//获得要下载的File对象
		File file = new File(path + File.separator + downloadFilePath);
		HttpHeaders headers = new HttpHeaders();//http头信息
		String downloadFileName = new String(downloadFilePath.getBytes("UTF-8"), "iso-8859-1");//设置编码
		headers.setContentDispositionFormData("attachment", downloadFileName);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		//MediaType:互联网媒介类型  contentType：具体请求中的媒体类型信息
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);

	}
}
